#include "superdialog.h"



SuperDialog::SuperDialog()
{
    //设置窗口风格
    QPalette p=this->palette();
    p.setColor(QPalette::Window,Qt::yellow);    //设置颜色为黄色
    setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);   //无框且始终停留在顶部
    setWindowOpacity(0.8);  //设置透明度
    setPalette(p);

    layout=new QVBoxLayout(this);

    closeLabel=new QLabel(tr("×")); //关闭按钮
    closeLabel->installEventFilter(this);   //增加事件监听器
    layout->addWidget(closeLabel,0,Qt::AlignRight);
}

void SuperDialog::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons()&Qt::LeftButton)
    {
        move(event->globalPos()-dragPosition);
        event->accept();
    }
}

void SuperDialog::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        dragPosition=event->globalPos()-frameGeometry().topLeft();
        event->accept();
    }
}

bool SuperDialog::eventFilter(QObject * watched, QEvent * event)
{
    if(event->type()==QEvent::MouseButtonPress) //如果是单击鼠标
    {
        QMouseEvent *mouseEvent=(QMouseEvent *)event;   //转换成鼠标事件
        if(mouseEvent->button()&Qt::LeftButton) //如果单击的是左键
        {
            if(watched==this->closeLabel)   //如果点击了关闭
            {
                close();    //关闭
            }
        }
    }
    return QDialog::eventFilter(watched,event);
}
